package order_service

import (
	"bee/pkg/logging"
	"bee/pkg/util"
	"github.com/go-resty/resty/v2"
	"github.com/tidwall/gjson"
	"strconv"
)

type DepositBill struct {
	CustomerID     string `json:"customerId"`
	IdentityID     string `json:"identityId"`
	AccountID      int    `json:"accountId"`
	DepositAmount  string `json:"depositAmount"`
	CouponAmount   string `json:"couponAmount"`
	EffectiveStart string `json:"effectiveStart"`
	EffectiveEnd   string `json:"effectiveEnd"`
	OperatorID     string `json:"operatorId"`
	SignerID       string `json:"signerId"`
	Remark         string `json:"remark"`
	CouponRemark   string `json:"couponRemark"`
}

type InvoiceInfo struct {
	InvoiceType      string `json:"invoiceType"`
	InvoiceTitleType string `json:"invoiceTitleType"`
	Title            string `json:"title"`
}

type DeliveryInfo struct {
	ReceiverMobile string `json:"receiverMobile"`
	ReceiverEmail  string `json:"receiverEmail"`
}

type RemittancePay struct {
	RemittanceID string `json:"remittanceId"`
	Amount       int    `json:"amount"`
}

type Deposit struct {
	DepositBill `json:"depositBill"`
	InvoiceInfo	`json:"invoiceInfo"`
	DeliveryInfo `json:"deliveryInfo"`
	PaymentWay  string `json:"paymentWay"`
	RemittancePayList []RemittancePay `json:"remittancePayList"`
}

func (o Order) CreateDepositBill(depositType string ,host string) (string, error) {
	url := host + "/api/opc/deposit/createDepositBill"

	var deposit Deposit
	if depositType == "cash" {
		deposit = o.setCashDepositBill(o.CashNum)
	} else {
		deposit = o.setCouponDepositBill(o.CouponNum)
	}

	logging.ZapLogger.Infow("充值单参数", "deposit", deposit, "traceId", o.TraceId)

	headerData := make(map[string]string)
	headerData["Content-Type"] = "application/json;charset=UTF-8"

	cookie := util.SetCookie("sso-epctoken", o.Token)

	client := resty.New()
	resp, depositErr := client.R().SetCookie(cookie).
		SetHeaders(headerData).SetBody(deposit).
		Post(url)

	if depositErr != nil {
		return "", depositErr
	}
	logging.ZapLogger.Infow("创建充值单", "result", resp, "traceId", o.TraceId)
	depositId := gjson.GetBytes(resp.Body(), "data.depositId").String()
	return depositId, nil
}

func (o Order) setCashDepositBill(amount string) Deposit {

	depositBill := DepositBill{
		CustomerID:     o.CustomerId,
		IdentityID:     o.IdentityId,
		AccountID:      o.AccountId,
		DepositAmount:  amount,
		CouponAmount:   "",
		EffectiveStart: "",
		EffectiveEnd:   "",
		OperatorID:     "1B2C20B854FE4B588A87F4CABE6DEF6D",
		SignerID:       "1B2C20B854FE4B588A87F4CABE6DEF6D",
		Remark:         "",
	}

	invoiceInfo := InvoiceInfo{
		InvoiceType:      "4",
		InvoiceTitleType: "2",
		Title:            o.FullName,
	}

	deliveryInfo := DeliveryInfo{
		ReceiverMobile: o.ReceiverMobile,
		ReceiverEmail: o.ReceiverEmail,
	}

	paymentWay := "1"

	cashAmount, _ := strconv.Atoi(o.CashNum)
	pay := RemittancePay{
		RemittanceID: o.RemittanceId,
		Amount:       cashAmount,
	}

	var remittancePayList []RemittancePay
	remittancePayList = append(remittancePayList, pay)

	cashOrder := Deposit{
		DepositBill:       depositBill,
		InvoiceInfo:       invoiceInfo,
		DeliveryInfo:      deliveryInfo,
		PaymentWay:        paymentWay,
		RemittancePayList: remittancePayList,
	}
	return cashOrder
}

func (o Order) setCouponDepositBill(amount string) Deposit {
	depositBill := DepositBill{
		CustomerID:     o.CustomerId,
		IdentityID:     o.IdentityId,
		AccountID:      o.AccountId,
		DepositAmount:  "",
		CouponAmount:   amount,
		EffectiveStart: "",
		EffectiveEnd:   "",
		OperatorID:     "1B2C20B854FE4B588A87F4CABE6DEF6D",
		SignerID:       "1B2C20B854FE4B588A87F4CABE6DEF6D",
		CouponRemark: "bee自动造号卡券充值备注",
	}

	order := Deposit{
		DepositBill:       depositBill,
		PaymentWay:        "",
		RemittancePayList: nil,
	}

	return order
}

type Audit struct {
	CustomerID string `json:"customerId"`
	DepositID  string `json:"depositId"`
	AuditFlag  bool   `json:"auditFlag"`
	Remark     string `json:"remark"`
}

func (o Order) Audit(host string) error {

	url := host + "/api/opc/deposit/audit"
	audit := Audit{
		CustomerID: o.CustomerId,
		DepositID:  o.CouponDepositId,
		AuditFlag:  true,
		Remark:     "同意",
	}

	headerData := make(map[string]string)
	headerData["Content-Type"] = "application/json;charset=UTF-8"

	cookie := util.SetCookie("sso-epctoken", o.Token)

	client := resty.New()
	resp, auditErr := client.R().SetCookie(cookie).
		SetHeaders(headerData).SetBody(audit).
		Post(url)
	logging.ZapLogger.Infow("卡券充值单审核结果", "audit", audit, "resp", resp, "traceId", o.TraceId)
	if auditErr != nil {
		return auditErr
	}

	return nil
}